package com.jsmtr.rms.utils;

import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.entity.BaseConfig;
import com.jsmtr.rms.service.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

public class FilePathUtil {
    private volatile static FilePathUtil filePathUtil;


    @Autowired
    private IConfigService configService;
    private String imgpath;
    private String mappath;

    public FilePathUtil() {
    }

    private void updatePaths() {
        try {
            List<BaseConfig> pathconfigs = configService.getConfigs("path");
            for (BaseConfig baseConfig :
                    pathconfigs) {
                String code = baseConfig.getCode();
                if ("imgpath".equalsIgnoreCase(code)) {
                    this.imgpath = baseConfig.getCont();
                } else if ("mappath".equalsIgnoreCase(code)) {
                    this.mappath = baseConfig.getCont();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getImgpath() {
        if (this.imgpath == null) {
            updatePaths();
        }
        return imgpath;
    }

    public void setImgpath(String imgpath) {
        this.imgpath = imgpath;
    }

    public String getMappath() {
        if (this.imgpath == null) {
            updatePaths();
        }
        return mappath;
    }

    public void setMappath(String mappath) {
        this.mappath = mappath;
    }

    public IConfigService getConfigService() {
        return configService;
    }

    public void setConfigService(IConfigService configService) {
        this.configService = configService;
    }
}
